<!-- 待支付-详情 -->
<template>
  <div class="index-container">
    <div class="head_djs p15">
      请在
      <van-count-down :time="time" class="head_djs_span" />内支付，逾期订单将会失效
    </div>

    <div class="jiange"></div>

    <div class="p15 head_djs dingdanhao">
      订单号：{{ list.sn }}
      <van-button round class="textoverflow1 yuyueren">{{ list.sale_num }}人已报名</van-button>
    </div>
    <div class="banxing">
      <div class="imgs">
        <img :src="list.image" alt />
      </div>
      <div>
        <div class="banxing_title">{{ list.name }}</div>
        <div class="banxing_sku">{{ list.sku_name }}</div>
        <div class="grid_time">
          <span class="grid_shoujia">¥{{ list.price }}</span>
          <span v-if="list.orig_price != '0.00'" class="grid_yuanjia">¥{{ list.orig_price }}</span>
          <span class="grid_status">待支付</span>
        </div>
      </div>
    </div>
    <van-field v-model="list.addtime" label="下单时间：" />

    <div class="jiange"></div>

    <van-field v-if="list.orig_price != '0.00'" v-model="list.orig_price" label="商品金额：" />
    <van-field v-model="list.price" label="实付金额：" class="shifu" />
    <div class="jiange"></div>

    <div class="headline clearfix" v-if="list.express_phone">
      <span class="h_left">收货信息</span>
      <!-- <span class="h_right" @click="toSelectAddress">
        去修改
        <van-icon name="arrow" />
      </span>-->
    </div>
    <div class="m15" v-if="list.express_phone" style="margin-bottom: 25px">
      <div class="name_phone">
        <!-- <van-icon :name="require('../../../assets/img/dizhi.png')" /> -->
        <span>{{ list.express_name }}</span>
        <span>{{ list.express_phone }}</span>
      </div>
      <div class="dizhi">
        {{ list.express_province }}
        <span>{{ list.express_city }}</span>
        <span>{{ list.express_town }}</span>
        <span>{{ list.express_addr }}</span>
      </div>
    </div>

    <div class="jiange"></div>

    <div class="headline clearfix">
      <span class="h_left">支付方式</span>
    </div>

    <!-- <van-checkbox-group v-model="checked" style="padding-bottom:90px">
      <van-cell-group>
        <van-cell
          v-for="(item, index) in list"
          clickable
          :key="item"
          :icon="require('../../../assets/img/weixinzhifu.png')"
          title="微信支付"
          @click="toggle(index)"
        >
          <template #right-icon>
            <van-checkbox :name="item" :ref="el => checkboxRefs[index] = el" @click.stop />
          </template>
        </van-cell>
      </van-cell-group>
    </van-checkbox-group>-->
    <van-radio-group v-model="checked">
      <van-cell-group>
        <van-cell
          title="微信支付"
          :icon="require('@/assets/images/weixinzhifu.png')"
          clickable
          @click="checked = '1'"
        >
          <template #right-icon>
            <van-radio name="1" />
          </template>
        </van-cell>
      </van-cell-group>
    </van-radio-group>

    <van-submit-bar
      label="应支付："
      :decimal-length="2"
      :disabled="isDisabledSubmitBtn"
      :price="parseFloat(list.price) * 100"
      button-text="立即支付"
      @submit="onSubmit"
      button-color="#009697"
    />
  </div>
</template>

<script>
import moment from 'moment'
// import { weixinPay } from '@/utils/weixinPay.js'
import { detailOrders, directPayOrders } from '@/api/mine/order'
export default {
  data() {
    return {
      isDisabledSubmitBtn: false,
      time: 0,
      dingdanhao: '2020112900876573',
      xiaodan: '2018-05-25  15:08:23',
      zhifu: '2018-05-25  15:08:23',
      zhifufangshi: '微信支付',
      shangpinjine: '¥11800',
      shifujine: '¥10800',
      name: '王派学员1    18812345678',
      dizhi: '湖北省武汉市洪山区关山街道光谷时代广场A座1102',
      list: {},
      // checked: [],
      checkboxRefs: [],
      checked: '1',
      weChatParameter: {}
    }
  },

  computed: {},

  mounted() {
    this.fetchList()
  },

  methods: {
    // 获取个人信息
    fetchList() {
      const data = {
        sn: JSON.parse(decodeURIComponent(this.$route.query.row)).sn
      }
      detailOrders(data).then(res => {
        this.list = res.data
        this.time = (this.list.addtime + this.list.pay_overtime - this.list.server_time) * 1000
        this.list.addtime = moment(this.list.addtime * 1000).format('YYYY-MM-DD HH:mm:ss')
      })
    },
    toSelectAddress() {
      this.$router.push({ name: 'SelectAddress', params: { id: this.addressList.id } })
    },
    toggle(index) {
      this.checkboxRefs.value[index].toggle()
    },
    onSubmit() {
      const data = {
        sn: JSON.parse(decodeURIComponent(this.$route.query.row)).sn
      }
      this.isDisabledSubmitBtn = true
      directPayOrders(data)
        .then(res => {
          this.weChatParameter = res.data.orderinfo
          this.weixinPay()
        })
        .catch(error => {
          this.isDisabledSubmitBtn = false
          this.$toast(error.msg)
        })
    },

    // h5方法调取微信支付
    weixinPay(params) {
      var that = this
      if (typeof WeixinJSBridge === 'undefined') {
        if (document.addEventListener) {
          document.addEventListener('WeixinJSBridgeReady', that.onBridgeReady(params), false)
        } else if (document.attachEvent) {
          document.attachEvent('WeixinJSBridgeReady', that.onBridgeReady(params))
          document.attachEvent('onWeixinJSBridgeReady', that.onBridgeReady(params))
        }
      } else {
        that.onBridgeReady()
      }
    },
    // 微信内置浏览器类，weChatParameter对象中的参数是3.步骤代码中从后端获取的数据
    onBridgeReady() {
      var that = this
      // var timestamp = Math.round(that.weChatParameter.timeStamp).toString()
      WeixinJSBridge.invoke(
        'getBrandWCPayRequest',
        {
          debug: true,
          appId: that.weChatParameter.appId, // 公众号名称，由商户传入
          timeStamp: that.weChatParameter.timeStamp, // 时间戳，自1970年以来的秒数
          nonceStr: that.weChatParameter.nonceStr, // 随机串
          package: that.weChatParameter.package,
          signType: that.weChatParameter.signType, // 微信签名方式：
          paySign: that.weChatParameter.paySign, // 微信签名
          jsApiList: ['chooseWXPay']
        },
        function (res) {
          // 使用以上方式判断前端返回,微信团队郑重提示：res.err_msg将在用户支付成功后返回ok，但并不保证它绝对可靠。
          if (res.err_msg == 'get_brand_wcpay_request:ok') {
            // 支付成功后的操作
            that.$toast({
              message: '支付成功',
              duration: 2000
            })
            that.isDisabledSubmitBtn = true
          } else if (res.err_msg == 'get_brand_wcpay_request:cancel') {
            // 取消支付的操作
            that.$toast({
              message: '取消支付',
              duration: 2000
            })
            that.isDisabledSubmitBtn = false // 按钮恢复高亮
          } else {
            // 支付失败的操作
            that.$toast({
              message: '支付失败',
              duration: 2000
            })
            that.isDisabledSubmitBtn = false // 按钮恢复高亮
          }
        }
      )
    }
  }
}
</script>

<style lang="scss" scoped>
.jiange {
  height: 10px;
  background: #f7f7f7;
}

.head_djs {
  // width: 375px;
  height: 40px;
  background: #ffffff;
  font-size: 14px;
  font-weight: 500;
  color: #999999;
  line-height: 40px;
  .head_djs_span {
    display: inline-block;
    color: #fd953f;
  }
}

.dingdanhao {
  border-bottom: 1px solid #ddd;
  .yuyueren {
    float: right;
    margin-top: 6px;
    // width: 82px;
    padding: 0 6px;
    height: 25px;
    line-height: 25px;
    background: rgba(7, 120, 121, 0.1);
    font-size: 12px;
    color: #11a8a4;
  }
}

.banxing {
  padding: 15px;
  display: flex;
  border-bottom: 1px solid #f2f2f2;
  .imgs {
    margin-right: 9px;
    img {
      width: 112px;
      height: 75px;
      border-radius: 5px;
      overflow: hidden;
    }
  }
}
.divider {
  margin: 15px 0px;
}
.banxing_title {
  // height: 40px;
  font-size: 14px;
  font-family: PingFang SC;
  font-weight: bold;
  color: #333333;
  line-height: 21px;
  overflow: hidden;
  text-overflow: ellipsis;
  display: -webkit-box;
  -webkit-line-clamp: 1;
  -webkit-box-orient: vertical;
}
.banxing_sku {
  font-size: 14px;
  font-weight: 500;
  color: #333333;
}
.grid_time {
  margin-top: 10px;
  font-size: 12px;
  font-weight: 400;
  color: #999999;
  .grid_shoujia {
    margin-right: 5px;
    font-size: 16px;
    font-weight: bold;
    color: #fd953f;
    line-height: 30px;
  }
  .grid_yuanjia {
    font-size: 12px;
    font-weight: 500;
    text-decoration: line-through;
    color: #999999;
  }
  .grid_status {
    float: right;
    font-size: 14px;
    font-weight: 500;
    color: #fd953f;
  }
}

.headline {
  margin: 20px 15px 18px;
  .h_left {
    float: left;
    font-size: 14px;
    font-weight: bold;
    color: #333333;
  }
  .h_right {
    float: right;
    font-size: 11px;
    font-weight: bold;
    color: #009697;
    .van-icon {
      vertical-align: middle;
      margin-bottom: 2px;
    }
  }
}
.name_phone {
  font-size: 13px;
  font-weight: bold;
  color: #333333;
  .van-icon {
    vertical-align: text-top;
  }
  span {
    margin-left: 10px;
  }
}
.dizhi {
  margin: 20px 0 0 25px;
  font-size: 14px;
  font-weight: bold;
  color: #666666;
  line-height: 20px;
  span {
    margin-left: 10px;
  }
}

::v-deep .van-cell__title {
  color: #999999;
}
::v-deep .zhifu .van-field__control {
  color: #11a8a4 !important;
}
::v-deep .shifu .van-field__control {
  color: #fd953f !important;
}

::v-deep .van-radio-group {
  padding-bottom: 90px;
  img {
    width: 21px;
    height: 21px;
  }
  .van-cell__title {
    font-size: 13px;
    font-weight: 500;
    color: #2c2c2c;
  }
  .van-cell {
    padding: 10px 15px;
  }
  .van-hairline--top-bottom::after,
  .van-hairline-unset--top-bottom::after {
    border: 0;
  }
  .van-radio__icon--checked .van-icon {
    background-color: #009697;
    border-color: #009697;
  }
}

::v-deep .van-submit-bar {
  height: 70px;
  background: #ffffff;
  box-shadow: 0px -1px 4px 0px rgba(66, 66, 66, 0.19);
  .van-submit-bar__bar {
    height: 70px;
  }
  .van-submit-bar__price {
    color: #009697;
  }
  .van-button {
    width: 130px;
    height: 44px;
    line-height: 44px;
    background: #009697;
    border-radius: 22px;
    font-size: 16px;
  }
}
</style>
